package com.guigu.tms.security.controller;

import com.guigu.tms.commons.order.entity.vo.Result;
import com.guigu.tms.commons.security.entity.AuthUser;
import com.guigu.tms.commons.user.entity.Member;
import com.guigu.tms.security.config.ValidateCodeConfig;
import com.guigu.tms.security.service.IDriverService;
import com.guigu.tms.security.util.JwtUtils;
import com.guigu.tms.security.util.ValidateCode;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@RestController
@RequestMapping("/driver")
public class DriverAuthController {

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    ValidateCode DriverCode;

    @Autowired
    IDriverService iDriverService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping("login")
    public AuthUser login(String phone, String value,HttpServletResponse response){
        AuthUser login = iDriverService.Login(phone, value);
        login.setUid(login.getId().toString());
        //System.out.println(login);
        String key = DriverCode.getKey();
        String KeyValue=key+phone;
        if(login!=null){
            if(redisTemplate.hasKey(KeyValue)){
                if(redisTemplate.opsForValue().get(KeyValue).equals(value)){
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("phone",phone);
                    String jwt=jwtUtils.generateToken(map);
                    response.setHeader("driver",jwt);
                    //System.out.println(jwt);
                    return login;
                }
            }
        }
        return null;
    }

    @PostMapping("DriverCode")
    public boolean DriverCode(String phone){
        return DriverCode.getValidateCode(phone);
    }

    @PostMapping("checkAuth")
    public Result checkAuth(String token){
        Result result = null;
        HashMap<String, Object> map = new HashMap<>();
        if(StringUtils.isBlank(token)){
            result=Result.error("token无效！");
        }else {
            try {
                Claims claim = jwtUtils.getClaimByToken(token);
                map.put("msg","验证成功！");
                map.put("data",claim);
                result=Result.ok(map);
            } catch (ExpiredJwtException e) {
                map.put("msg","凭证过期");
                map.put("status",401);
                result=Result.ok(map);
            } catch (SignatureException e) {
                map.put("msg","签名错误");
                map.put("status",402);
                result=Result.ok(map);
            } catch (MalformedJwtException e) {
                map.put("msg","格式错误");
                map.put("status",403);
                result=Result.ok(map);
            }
        }
        System.out.println(result);
        return result;
    }
}
